<template>
	<view class="goods-detail">
		<!-- 商品轮播图 -->
		<swiper indicator-dots autoplay>
			<block v-for="item in images" :key="item.src">
				<swiper-item>
					<image :src="item.src"></image>
				</swiper-item>
			</block>
		</swiper>
		
		<view class="box1">
			<view class="price">
				<text>￥{{info.sell_price}}</text>
				<text>￥{{info.market_price}}</text>
			</view>
			<view class="name">{{info.title}}</view>
		</view>
		
		<view class="line"></view>
		
		<view class="box2">
			<view>货号：{{info.goods_no}}</view>
			<view>库存：{{info.stock_quantity}}</view>
		</view>
		
		<view class="line"></view>
		
		<view class="box3">
			<view class="title">详情介绍</view>
			<view class="content">
				<rich-text :nodes="detail.content"></rich-text>
			</view>
		</view>
		
		<uni-goods-nav class="goods-nav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
	</view>
</template>

<script>
	import uniGoodsNav from '@/components/uni-goods-nav/uni-goods-nav.vue'
	import {getSwipersForDetail, getGoodsInfo, getDetailContent} from '../../../api/index/goods.js'
	export default {
		data() {
			return {
				images: [],
				info: {},
				detail: {},
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
					info: 2,
					infoBackgroundColor:'#007aff',
					infoColor:"red"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: '#ff0000',
					color: '#fff'
				},
				{
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#fff'
				}]
			}
		},
		methods: {
			/**
			 * 获取商品的轮播图
			 * @param {Object} id 商品id
			 */
			getSwipersForDetail(id) {
				getSwipersForDetail(id).then(res => {
					this.images = res.data.message;
				})
			},
			/**
			 * 获取商品的信息
			 * @param {Object} id 商品id
			 */
			getGoodsInfo(id) {
				getGoodsInfo(id).then(res => {
					this.info = res.data.message[0];
				})
			},
			getDetailContent(id) {
				getDetailContent(id).then(res => {
					console.log(res)
					this.detail = res.data.message[0];
				})
			},
			onClick (e) {
				uni.showToast({
					title: `点击${e.content.text}`,
					icon: 'none'
				})
			},
			buttonClick (e) {
				console.log(e)
				this.options[2].info++
			}
		},
		onLoad(options) {
			let id = options.id
			this.getSwipersForDetail(id);
			this.getGoodsInfo(id);
			this.getDetailContent(id)
		},
		components: {
			uniGoodsNav
		}
	}
</script>

<style scoped lang="scss">
	@import '~@/static/css/index/detail.scss';
</style>
